package org.gourpg.entity.user;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.proxy.HibernateProxy;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.sql.Timestamp;
import java.util.Objects;

/**
 * 登录用户配置实体
 */
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@Entity
@Table(name = "user_detail")
public class UserDetails {
    /**
     * 纯ID，新用户临时生成，person表主键
     */
    @Id
    private Long personId;

    /**
     * 登录用名字
     */
    private String loginCode;

    /**
     * 密码（密钥类型全系统统一）默认abc123
     */
    private String psw;

    /**
     * 更新时间戳
     */
    private Timestamp updateTime;

    /**
     * 错误次数
     */
    private Integer errorNumber;

    /**
     * 最近一次登录时间
     */
    private Timestamp lastLoginTime;

    /**
     * 是否已锁定(Y/N)
     */
    private String isLocked;

    /**
     * 是否已登录(Y/N)
     */
    private String isLogined;

    @Override
    public final boolean equals(Object o) {
        if (this == o) return true;
        if (o == null) return false;
        Class<?> oEffectiveClass = o instanceof HibernateProxy ? ((HibernateProxy) o).getHibernateLazyInitializer().getPersistentClass() : o.getClass();
        Class<?> thisEffectiveClass = this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass() : this.getClass();
        if (thisEffectiveClass != oEffectiveClass) return false;
        UserDetails that = (UserDetails) o;
        return getPersonId() != null && Objects.equals(getPersonId(), that.getPersonId());
    }

    @Override
    public final int hashCode() {
        return this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass().hashCode() : getClass().hashCode();
    }
}
